import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const actions = {};
const mutations = {
	handleUserInfo: (state, userinfo) => {
		state.userinfo = userinfo;
		// 把登录的用户的名保存到localStorage中，防止页面刷新，导致vuex重新启动，用户名就成为初始值（初始值为空）的情况
		localStorage.setItem('userinfo', JSON.stringify(state.userinfo));
	}
};
const state = {
	userinfo: null || JSON.parse(localStorage.getItem('userinfo'))
};
// getters 只会依赖 state 中的成员去更新
const getters = {
	userName: (state) => state.userinfo
};

const store = new Vuex.Store({
	actions,
	mutations,
	state,
	getters
});
export default store;
